/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.controler;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import samoloty.model.IDataModel;
import samoloty.view.AcRegPanel;
import samoloty.view.MainWindow;
import samoloty.view.MyButton;
import samoloty.view.MyLabel;

/**
 *
 * @author borysfan
 */
public class AcRegPanelControler implements MyControler {

    private ArrayList<IDataModel> dataModels = null;
    private AcRegPanel panel = null;
    private Controler controler = null;

    public AcRegPanelControler(Controler c){
        controler = c;
        initPanel();
    }

    public void initPanel(){
        panel = new AcRegPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    }

    public void setDataModels(ArrayList<IDataModel> models){
        dataModels = models;
        initActions();
    }

    public void initActions(){
        clearWindow();
        panel.initScroll();
        int btnHeight = 40;
        btnHeight=30;
        if(panel!=null){
        panel.setTravelStart();
            panel.addTitle("SELECT AC REG");
            panel.insertVerticalSeparator(20);
            if(dataModels!=null){
                for(int i=0;i<dataModels.size();i++){
                    String subTitle = dataModels.get(i).getLabel();
                    panel.addSubTitle(subTitle);
                    IDataModel data = dataModels.get(i);
                    ArrayList<String> acReg = data.getAcReg();
                    for(int j=0;j<acReg.size();j++){
                        MyButton btn = panel.addButton(acReg.get(j),dataModels.get(i),btnHeight);
                        panel.insertVerticalSeparator(20);
                        AcRegDataBtnListener acRegListener = new AcRegDataBtnListener();
                        acRegListener.setModel(data);
                        acRegListener.setAcRegNum(acReg.get(j));
                        acRegListener.setTitle(subTitle);
                        btn.addActionListener(acRegListener);
                        panel.addButtonToTravel(btn);
                    }
                }
            }
        panel.setTravelStop();
        }
    }

    private int getAllBtnsAmount(){
        int result = 0;
        if(dataModels!=null){
            for(int i=0;i<dataModels.size();i++){
                result += dataModels.get(i).getAcReg().size();
            }
        }
        return result;
    }

    private void clearWindow(){
        Component [] comp = panel.getComponents();
        if(comp!=null){
            for(int i=0;i<comp.length;i++){
                panel.remove(comp[i]);
            }
        }

    }

    public void setUpWindow() {
        MainWindow window = controler.getView();
        window.setAllBtnStyle(MyButton.ACTIVE_BUTTON);
        window.setLeftPanelVisible(false);
        window.setDisplayVisible(false);
        window.setPrintVisible(false);
        window.setSaveVisible(false);
   
    }

    public AcRegPanel getPanel(){
        return panel;
    }

    public void firstFocused() {
        panel.firstFocused();
    }

    private class AcRegDataBtnListener implements ActionListener{

        private IDataModel model;
        private String text;
        private String title;
        
        public void setModel(IDataModel m){
            model = m;
        }

        public void setAcRegNum(String s){
            text = s;
        }

        public void setTitle(String s){
            title = s;
        }

        public void actionPerformed(ActionEvent e) {
                controler.setDataModel(model);
                controler.getDowPanelControler().setAcRegText(text);
                controler.setTitleBar(title+"   "+text);
                System.out.println(title);
                controler.setFlightPanel();
                controler.set_vacreg(text);
               
        }

    }
}
